package com.hogan.basiclibrary.base.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hogan.basiclibrary.R;
import com.hogan.basiclibrary.base.app.BaseApplication;
import com.hogan.basiclibrary.base.baseApi.utils.LogUtil;
import com.hogan.basiclibrary.base.utils.SharedPreferencesUtils;
import com.tbruyelle.rxpermissions.RxPermissions;

import rx.functions.Action1;

/**
 * Created by chenhu on 2017/10/13.
 */

public abstract class BaseAppCompatActivity extends AppCompatActivity {
    protected BaseApplication mBaseApplication;
    protected SharedPreferencesUtils mSharedPreferencesUtils;
    protected Context mContext;
    protected FragmentManager fragmentManager;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int resourceId = getLayoutResourceId();
        if (0 != resourceId) {
            setContentView(resourceId);
        }
        mContext = this;
        mBaseApplication = (BaseApplication) BaseApplication.getInstance();
        mSharedPreferencesUtils = SharedPreferencesUtils.getInstance(mBaseApplication.getBaseContext());
        fragmentManager = getSupportFragmentManager();
        ActivityUtils.addActivity(this);
        LogUtil.e(getClass().getSimpleName());
        initActivity();
    }

    protected abstract int getLayoutResourceId();

    protected abstract void initActivity();

    public void shortToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    protected void longToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    protected void shortSnack(String message) {
        if (null != getRootView()) {
            Snackbar snackbar = Snackbar.make(getRootView(), message, Snackbar.LENGTH_SHORT);
            snackbar.getView().setBackgroundColor(ContextCompat.getColor(this, R.color.abc_tint_switch_thumb));
            snackbar.show();
        }
    }

    public View getRootView() {
        return ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
    }

    /**
     * 6.0系统以上需要动态权限
     * 获取动态权限方法
     *
     * @param permission 具体的权限(如 Manifest.permission.CAMERA【相机权限】)
     * @param activity
     **/
    @RequiresApi(api = Build.VERSION_CODES.M)
    protected void getAppPermission(String permission, final Class<?> activity) {
        int hasPermission = checkSelfPermission(permission);
        if (hasPermission != PackageManager.PERMISSION_GRANTED) {
            if (!shouldShowRequestPermissionRationale(permission)) {
                showPermissionDialog("请在设置-应用-权限中开启相关权限。",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Uri packageURI = Uri.parse("package:" + mContext.getPackageName());
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                startActivity(intent);
                            }
                        });
                return;
            }
            RxPermissions.getInstance(mContext)
                    .request(permission)
                    .subscribe(new Action1<Boolean>() {
                        @Override
                        public void call(Boolean aBoolean) {
                            if (aBoolean) {
                                ActivityUtils.skipActivity(mContext, activity);
                            }
                        }
                    });
        }
    }

    private void showPermissionDialog(String message, DialogInterface.OnClickListener mListener) {
        new AlertDialog.Builder(mContext)
                .setTitle("权限申请")
                .setMessage(message)
                .setPositiveButton("去开启", mListener)
                .setNegativeButton("取消", null)
                .create()
                .show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtils.finishActivity(this);
    }
}
